﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Xml.Linq;
using com.eforceglobal.QuickSearch.Command.DTO;

namespace com.eforceglobal.QuickSearch.Command.ConfigParser
{
    public static class CommandConfigParser
    {
        public static string GetCommandTypeByName(string commandName)
        {
            commandName = commandName.ToLower();
            XElement searchNodeConfig = GetQuickSearchCommandConfigXml().Element("CommandNodes");
            XElement desiredNode =
                searchNodeConfig.Elements("CommandNode").Where(ele => ele.Attribute("Name").Value.ToLower().Equals(commandName)).
                    FirstOrDefault();
            if (desiredNode == null) throw new Exception("Command name not found!");
            return desiredNode.Attribute("Class").Value + "," + desiredNode.Attribute("Dll").Value;
        }
        public static List<QuickSearchItem> GetQuickSearchItems()
        {
            XElement seachNodeConfig = GetQuickSearchConfigXml();
            List<QuickSearchItem> quickSearchItems = new List<QuickSearchItem>();
            foreach (XElement element in seachNodeConfig.Elements("SearchNode"))
            {
                QuickSearchItem item = new QuickSearchItem();
                item.Caption = element.Attribute("Caption").Value;
                item.Roles = element.Attribute("Roles").Value.Split(',').ToList();
                item.Url = element.Attribute("Url").Value;
                item.Target = element.Attribute("Target").Value;
                var xAttribute = element.Attribute("Feature");
                if (xAttribute != null) item.FeatureName = xAttribute.Value;
                quickSearchItems.Add(item);
            }
            return quickSearchItems;
        }

        private static XElement GetQuickSearchConfigXml()
        {
            XElement xNav = (XElement)HttpRuntime.Cache.Get("QuickSearchConfig");
            if (xNav == null)
            {
                string fileName = GetApplicationPath() + ConfigurationManager.AppSettings["SiteConfigurationDirectory"] + "/QuickSearch.config";

                XDocument xDoc = XDocument.Load(fileName);
                xNav = xDoc.Element("SearchNodes");
                HttpRuntime.Cache.Add("QuickSearchConfig", xNav, new System.Web.Caching.CacheDependency(fileName), DateTime.Now.AddDays(7),
                        TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, null);
            }
            return xNav;
        }

        public static List<QuickSearchCommandItem> GetQuickSearchCommandNodeItems()
        {
            XElement searchNodeConfig = GetQuickSearchCommandConfigXml().Element("CommandNodes");
            List<QuickSearchCommandItem> quickSearchItems = new List<QuickSearchCommandItem>();
            foreach (XElement element in searchNodeConfig.Elements("CommandNode"))
            {
                QuickSearchCommandItem item = new QuickSearchCommandItem();
                item.Name = element.Attribute("Name").Value;
                item.Class = element.Attribute("Class").Value;
                item.Dll = element.Attribute("Dll").Value;
                item.Roles = element.Attribute("Roles").Value.Split(',').ToList();
                item.Target = element.Attribute("Target").Value;
                item.Help = element.Attribute("Help").Value;
                quickSearchItems.Add(item);
            }
            return quickSearchItems;
        }
        private static XElement GetQuickSearchCommandConfigXml()
        {
            XElement xNav = (XElement)HttpRuntime.Cache.Get("CommandConfig");
            if (xNav == null)
            {
                string fileName = GetApplicationPath() + ConfigurationManager.AppSettings["SiteConfigurationDirectory"] + "/Command.config";

                XDocument xDoc = XDocument.Load(fileName);
                xNav = xDoc.Element("Configuration");
                HttpRuntime.Cache.Add("CommandConfig", xNav, new System.Web.Caching.CacheDependency(fileName), DateTime.Now.AddDays(7),
                        TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, null);
            }
            return xNav;
        }
        private static string GetApplicationPath()
        {
            string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
            if (path.ToLower().Contains("bin") && path.ToLower().Contains("file"))
            {
                string strPath = path.TrimStart('f', 'i', 'l', 'e', ':', '\\', '\\');
                string finalPath = strPath.EndsWith("bin") ? strPath.TrimEnd('b', 'i', 'n') : strPath.TrimEnd('B', 'i', 'n');
                path = finalPath;
            }
            return path;
        }
    }
}
